﻿var lsInfo = {
  current: 1,
  pageSize: allPageSize,
  data: [],
  totalRow: 0,
  totalPage: 0,
  oneDate: {},
  editState: false
};

// document.getElementById('nav').innerHTML = template('navs', navinfo2);

$('#nav li').eq(2).addClass('active');

var getList = function (obj) {
  $.ajax({
    cache: false,
    async: false,
    url: '/api/CommonApi?API=GetTeacherApplyByRole',
    type: 'get',
    data: {
      ProjectName: searchObj.ProjectName,
      StartDate: searchObj.StartDate,
      EndDate: searchObj.EndDate,
      sorts: ' SubmitTime DESC',
      pagesize: obj.size,
      page: obj.index
    },
    dataType: 'json',
    success: function (data) {
      // console.log(data)
      if (data.Success) {
        obj.callback(data)
      }
    }
  });
};

getList({
  size: lsInfo.pageSize,
  index: lsInfo.current,
  callback: function (data) {
    // console.log(data)
    // console.log(JSON.stringify(data));
    var newData = data.ApiParamObj[0];
    for (var key in newData) {
      newData[key].ApplyDate = new Date(newData[key].ApplyDate).pattern('yyyy/MM/dd');
      if (newData[key]['State'] === '????') {
        newData[key]['State'] = '';
      }
    }
    // 列表数据
    document.getElementById('listTable').innerHTML = template('l23List', newData);
    // 分页
    var pagesInfo = data.ApiParamObj[1];
    lsInfo.data = newData;
    lsInfo.totalRow = pagesInfo[0].totalRow;
    lsInfo.totalPage = pagesInfo[0].totalPage;

    $('#totalRow').html(lsInfo.totalRow);
    pagesSet(lsInfo);
    setPageSize(lsInfo);
    loading.hide();
  }
});

// 点击分页请求事件
var showPage = function (o) {
  // console.log(o)
  getList({
    size: o.pageSize,
    index: o.current,
    callback: function (data) {
      // console.log(data)
      // console.log(JSON.stringify(data));
      var newData = data.ApiParamObj[0];
      for (var key in newData) {
        if (newData[key]['State'] === '????') {
          newData[key]['State'] = '';
        }
      }
      // 列表数据
      document.getElementById('listTable').innerHTML = template('l23List', newData);
      // 分页
      var pagesInfo = data.ApiParamObj[1];
      o.data = newData;
      o.totalRow = pagesInfo[0].totalRow;
      o.totalPage = pagesInfo[0].totalPage;

      $('#totalRow').html(o.totalRow);
      pagesSet(o);
      loading.hide();
    }
  });
};


// 编辑弹框
$('#table').on('click', '.view', function () {
  var index = $(this).parent().parent().index();
  lsInfo.oneDate = lsInfo.data[index];
  var v = lsInfo.oneDate;
  var allData = {
    data: [],
    data2: []
  };
  // console.log(JSON.stringify(v))

  // document.getElementById('viewDetail').innerHTML = template('detail', v);
  /*$.ajax({
    url: '/api/CommonApi?API=GetTeacherApply',
    type: 'get',
    data: {
      TeacherApplyID: v.TeacherApplyID
    },
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        console.log(data)
        // console.log(JSON.stringify(data));
        var obj = {};
        obj.ProjectName = v.ProjectName;
        obj.TypeName = v.TypeName;
        obj.BrandName = v.BrandName;
        obj.StageName = v.StageName;
        obj.SubmitTime = v.SubmitTime;
        obj.UserName = v.UserName;
        obj.data = data.ApiParamObj;
        document.getElementById('viewDetail').innerHTML = template('detail', obj);
      }
    }
  });*/
  loading.show();
  $.ajax({
    url: '/api/CommonApi?API=GetTeacherApply',
    type: 'get',
    data: {
      TeacherApplyID: v.TeacherApplyID
    },
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        allData.data = data.ApiParamObj;
        rend(allData);
      }
    }
  });

  $.ajax({
    url: '/api/CommonApi?API=GetTeacherApplyLog',
    type: 'get',
    data: {
      TeacherApplyID: v.TeacherApplyID
    },
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        // console.log(data);
        var datas= data.ApiParamObj;
        var key = {};
        var newData = [];
        var i = 0;
        var l = datas.length;
        for (;i < l;i++) {//console.log(datas[i].TriggerMan.indexOf('?') === -1 && key[datas[i].TriggerMan])
          if (datas[i].TriggerMan.indexOf('?') === -1 && !key[datas[i].TriggerMan] ) {
            key[datas[i].TriggerMan] = true;
            newData.push(datas[i]);
            // console.log(datas[i])
          }
        }
        // console.log(newData)
        allData.data2 = newData;
        rend(allData);
      }
    }
  });

  function rend(o) {
    if (!o.data.length || !o.data2.length) {
      return false;
    }
    console.log(o)
    o.ProjectName = v.ProjectName;
    o.TypeName = v.ProjectType;
    o.BrandName = v.ProjectBrand;
    o.StageName = v.State;
    o.SubmitTime = v.SubmitTime;
    o.UserName = v.UserName;

    checkTeacher(o);
    document.getElementById('viewDetail').innerHTML = template('detail', o);
    loading.hide();
    $('#detailList').modal('show');
  }

  // $('#detailList').modal('show');
});

// 通过
$('#viewDetail').on('click', '#pass', function () {
  $('#detailList').modal('hide');
  loading.show();
  // console.log(lsInfo.oneDate.TeacherApplyID)
  $.ajax({
    url: '/api/CommonApi?API=TeacherApplyAuditByRole',
    type: 'get',
    data: { TeacherApplyID: lsInfo.oneDate.TeacherApplyID, Role: 'PM', State:'3_PM_PASS', Explain: '审核说明'},
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        console.log('pass success');
        showPage(lsInfo);
        $('#detailList').modal('hide');
        loading.hide();
      }
    }
  });
});

// 驳回
$('#viewDetail').on('click', '#reject', function () {
  $('#detailList').modal('hide');
  $('#rejectReason').modal('show');
  // loading.show();
  /*$.ajax({
    url: '/api/CommonApi?API=TeacherApplyAuditByRole',
    type: 'get',
    data: { TeacherApplyID: lsInfo.oneDate.TeacherApplyID, Role: 'PM', State:'-1_REJECT', Explain: '审核说明'},
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        console.log('reject success');
        showPage(lsInfo);
        $('#detailList').modal('hide');
        loading.hide();
      }
    }
  });*/
});

$('#returnReject').click(function () {
  $('#rejectReason').modal('hide');
  $('#detailList').modal('show');
});

$('#rejectConfirm').click(function () {
  var v = $('#rejectDetail').val();
  if (!v) {
    alert('请输入驳回理由！');
    return false;
  }
  loading.show();
  $.ajax({
    url: '/api/CommonApi?API=TeacherApplyAuditByRole',
    type: 'get',
    data: { TeacherApplyID: lsInfo.oneDate.TeacherApplyID, Role: 'PM', State:'-1_REJECT', Explain: v},
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        console.log('reject success');
        showPage(lsInfo);
        $('#rejectReason').modal('hide');
        loading.hide();
      }
    }
  });
});